package com.yl.navigation.ui

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.launcher.ARouter
import com.yl.common.base.BaseVMFragment
import com.yl.common.exp.Constants
import com.yl.common.network.BaseResp
import com.yl.common.network.BaseStateObserver
import com.yl.navigation.R
import com.yl.navigation.adapter.TreeAdapter
import com.yl.navigation.bean.TreeBean
import com.yl.navigation.databinding.FragmentTreeBinding
import com.yl.navigation.viewmodel.TreeViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 * 体系模块
 */
class TreeFragment : BaseVMFragment<FragmentTreeBinding>() {

    private val treeViewModel: TreeViewModel by viewModel()

    private val txList = arrayListOf<View>()
    private var oldPosition = -1
    lateinit var treeAdapter: TreeAdapter
    private val myData = arrayListOf<TreeBean>()
    private lateinit var lm: LinearLayoutManager


    override fun getLayoutID() = R.layout.fragment_tree

    override fun observe() {
        treeViewModel.treeBeanList.observe(this,
            object : BaseStateObserver<List<TreeBean>>(null) {
                override fun getRespDataSuccess(it: BaseResp<List<TreeBean>>) {
                    Log.d(TAG, "${it.data}")
                    for (i in it.data!!.indices) {
                        myData.add(it.data!![i])
                    }
                    addView(myData)
                    treeAdapter.setData(myData)
                    Log.e("-----------", myData.size.toString())
                    Log.d(TAG, "observe: update data")
                    dismissLoadingDialog()
                }
            })
    }


    private fun addView(list: List<TreeBean>) {
        for (i in list.indices) {
            val layout = LayoutInflater.from(activity).inflate(R.layout.item_sys, null, false)
            layout.tag = i
            layout.setOnClickListener {
                setColor(it.tag as Int)
                lm.scrollToPositionWithOffset(it.tag as Int, 0)
            }

            val t = layout.findViewById<TextView>(R.id.tx_sys)
            txList.add(t)
            t.text = list[i].name

            if (i == 0) {
                setColor(i)
            }

            mBind.llSys.addView(layout)
        }

    }

    private fun setColor(position: Int) {
        if (oldPosition == position) return
        if (oldPosition != -1) {
            txList[oldPosition].background =
                ResourcesCompat.getDrawable(resources, R.drawable.shape_10dp_white, null)
        }

        oldPosition = position
        txList[oldPosition].background =
            ResourcesCompat.getDrawable(resources, R.drawable.shape_10dp_grey, null)
    }

    override fun init() {
        showLoadingDialog()
        lm = LinearLayoutManager(activity)
        mBind.recyclerView.layoutManager = lm
        treeAdapter = TreeAdapter {
            val data = myData[it.p1].articles[it.p2]
            ARouter.getInstance()
                .build(Constants.PATH_WEB)
                .withString(Constants.WEB_LINK, data.link)
                .withString(Constants.WEB_TITLE, data.title)
                .navigation()
        }
        mBind.recyclerView.adapter = treeAdapter
        treeViewModel.getTree()
    }

    fun scrollToTop() {
        mBind.recyclerView.run {
            if (lm.findFirstVisibleItemPosition() > 20) {
                scrollToPosition(0)
            } else {
                smoothScrollToPosition(0)
            }
        }
    }
}